package tripx.blog;

import java.io.File;
import java.util.HashMap;
import tripx.server.Service;
import tripx.sql.SqlHandler;
import tripx.sql.UpdateSqlHandler;

/**
 * BlogService: provide methods for clients to manage blog data.
 * 
 * @author zhb
 */
public class BlogService extends Service {

	/**
	 * BlogProperty contains the common data which shared by all the instance of
	 * BlogService
	 */
	private BlogServiceConfig config;
	/**
	 * translate the request from the client
	 */
	private UpdateSqlHandler blogHandler;

	/**
	 * Get an instance of BlogService.
	 */
	public BlogService() {
		config = BlogServiceConfig.getInstance();
		blogHandler = new UpdateSqlHandler("blog");

		sqlHandler = new SqlHandler(config.getQueryActionList());
	}

	/**
	 * add a new blog to the database.
	 * 
	 * @param blog
	 * @return
	 */
	public boolean addBlog(DbBlog blog) {
		String sql = blogHandler.getSqlAdd(blog);
		int status = blogHandler.getStatus() & invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * delete a blog by pairs of key-value
	 * 
	 * @param kvMap
	 * @return
	 */
	public boolean deleteBlog(HashMap<String, String> kvMap) {
		String sql = blogHandler.getSqlDelete(kvMap);
		int status = blogHandler.getStatus() & invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * modify the blog, with a single attribute modification
	 * 
	 * @param key
	 *            which column
	 * @param oldValue
	 *            previous value
	 * @param newValue
	 *            current new value
	 * @return
	 */
	public boolean modifyBlog(String key, String newValue,
			HashMap<String, String> conditionMap) {
		String sql = blogHandler.getSqlModify(key, newValue, conditionMap);
		int status = blogHandler.getStatus() & invoker.executeUpdateSql(sql);
		if (status == 0) {
			return false;
		}
		return true;
	}

	/**
	 * upload an image to the server, you should specify the image name and the
	 * format name. there follows the format name description
	 * <table border="1" summary="Image format advantages and disadvantages">
	 * <tbody>
	 * <tr>
	 * <th id="h1">Format</th>
	 * <th id="h2">Plus</th>
	 * <th id="h3">Minus</th>
	 * </tr>
	 * <tr>
	 * <td headers="h1">GIF</td>
	 * <td headers="h2">Supports animation, and transparent pixels</td>
	 * <td headers="h3">Supports only 256 colors and no translucency</td>
	 * </tr>
	 * <tr>
	 * <td headers="h1">PNG</td>
	 * <td headers="h2">Better alternative than GIF or JPG for high colour
	 * lossless images, supports translucency</td>
	 * <td headers="h3">Doesn't support animation</td>
	 * </tr>
	 * <tr>
	 * <td headers="h1">JPG</td>
	 * <td headers="h2">Great for photographic images</td>
	 * <td headers="h3">Loss of compression, not good for text, screenshots, or
	 * any application where the original image must be preserved exactly</td>
	 * </tr>
	 * </tbody>
	 * </table>
	 * 
	 * @param imageData
	 *            the image data
	 * @param imageName
	 *            the image name
	 * @param formatName
	 *            the image format name
	 * @return
	 */
	public boolean uploadImage(byte[] imageData, String path, String formatName) {
		// the path is something like `userName/cityName/imageName.png`
		// check the folder
		String folderPath = path.substring(0, path.lastIndexOf('/'));
		File folder = new File(config.getImageBaseDirPath() + "/" + folderPath);
		if (!folder.exists()) {
			logger.info(folder.getAbsolutePath());
			folder.mkdirs();
		}
		return super.uploadImage(imageData, config.getImageBaseDirPath() + "/"
				+ path, formatName);
	}

	/**
	 * delete the specified Image.
	 * 
	 * @param imageName
	 * @return
	 */
	public boolean deleteImage(String path) {
		return super.deleteFile(config.getImageBaseDirPath() + "/" + path);
	}

	/**
	 * download the image from server according the image name and format name
	 * 
	 * @param imageName
	 *            the name of the image
	 * @param formatName
	 *            the format name
	 * @return
	 */
	public byte[] downloadImage(String path, String formatName) {
		return super.downloadImage(config.getImageBaseDirPath() + "/" + path,
				formatName);
	}

	/**
	 * get the blog image base directory
	 * 
	 * @return
	 */
	public String getImageBaseDirPath() {
		return config.getImageBaseDirPath();
	}
}
